package com.suray.wcs.service.newled;

import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.wcs.service.util.WcsUtil;

import java.io.IOException;

public class LedBean {

    /**
     * 服务器ip
     */
    private String ip;
    /**
     * 服务器端口
     */
    private int port;
    /**
     * led地址
     */
    private String address;

    public LedBean(String ip, int port, String address) {
        this.ip = ip;
        this.port = port;
        this.address = address;
    }

    /**
     * LED显示屏显示
     *
     * @param context  需要显示的内容
     * @param district 需要显示的区域 LED可以设置多个区域 第一个区域是01，第二个是02 以此类推
     * @throws IOException
     */
    public void sendMessage(String context, String district, String moveWay) {
        try {
            Datagram showData = Datagram.getShowData(address, district, moveWay, context);
            String showDatagram = showData.getShowDatagram();
            LoggerUtil.info("发送led报文-->" + showDatagram, this.getClass());
            String response;
            int count = 0;
            do {
                response = UDPComm.sendData(showDatagram.getBytes(), ip, port);
                if (count > 0) {
                    LoggerUtil.info("开始重发地址为-->" + address + "的led数据" + "重发次数-->" + count, this.getClass());
                }
                count++;
            } while (!response.equals("EQOK") && count <= 3);
        } catch (Exception e) {
            WcsUtil.notification("地址为-->" + address + "的led通讯异常");
            LoggerUtil.error("地址为-->" + address + "的led通讯异常", this.getClass());
        }
    }
}
